/**
 * @file MyArray.cpp
 * @author redfrogotr
 * @brief 设计整数数组类
 * @date 2022-04-03
 */

#include <iostream>
using namespace std;

class MyArray
{
private:
    int* elementData;
    int size;

public:
    explicit MyArray(int size, int initValue = 0) : size(size) {
        if (size <= 0) {
            cout << "[WARN] size <= 0 is FATAL, so we have to use 1 the default value of size to initialize it!\n";
            this->size = 1;
        }
        elementData = new int[this->size];
        for (int i = 0; i < this->size; ++i)
            elementData[i] = initValue;
    }
    int& operator[](int i) {
        if (i >= size && i < 0) {
            cout << "[WARN] array index out of bounds, so 0 the default index will be used!\n";
            return elementData[0];
        }
        return elementData[i];
    }
    MyArray& operator++() {
        for (int i = 0; i < size; ++i) {
            ++elementData[i];
        }
        return *this;
    }

    friend ostream& operator<<(ostream& out, MyArray& array);
};

ostream& operator<<(ostream& out, MyArray& array) {
    for (int i = 0; i < array.size; ++i) {
        out << array.elementData[i] << " ";
    }
    return out;
}

int main(int argc, char const *argv[])
{
    int a = retx();
    cout << a << " " << x << endl;

    MyArray a(10, -1), b(5);
    cout << a << endl << b << endl;

    for (int i = 0; i < 10; i++) {
        a[i] = i;
    }
    cout << a << endl;
    a[8] = a[1];
    a[11] = a[1];
    cout << a << endl;
    
    cout << ++b << endl;

    return 0;
}
